using System;
using System.Collections.Generic;
using System.Linq;

namespace Mbs.Elements
{
    [Serializable]
    class ElementFactoryRepository : MarshalAndDisposableObject, IElementFactoryRepository
    {
        public ElementFactoryRepository()
        {
            Factories = new List<IElementFactory>();
        }

        private List<IElementFactory> Factories;

        public IElementFactory GetFactory(object uiElement)
        {
            return Factories.FirstOrDefault(factory => factory.Supports(uiElement));
        }

        public void RegisterFactory(IElementFactory factory)
        {
            Factories.Add(factory);
        }

        public void Clear()
        {
            Factories.Clear();
        }

        protected override void Dispose(bool disposing)
        {
            Clear();
        }
    }
}
